﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using WebApi.Helpers;
using WebApi.Infrastructure;
using WebApi.Model;
using Microsoft.Extensions.Logging;

namespace WebApi.Controllers
{
    /// <summary>
    /// 此控制器来调试接口调用
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly ILogger<ValuesController> _logger;

        public ValuesController(ILogger<ValuesController> logger)
        {
            _logger = logger;
        }


        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        /// <summary>
        /// 获取用户信息--测试token带token为Admin登录信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("GetUsers_By_Token_Admin")]
        [ProducesResponseType(201)]
        [ProducesResponseType(400)]
        [Authorize(Roles = "Admin")]
        // GET: api/<ValuesController>
        public Users GetUsers_By_Token_Admin(string name)
        {
            var db = DbHelper.GetDbConn();
            var user = db.Queryable<Users>().Where(it => it.UserName.Equals(name)).First();
            return user;
        }

        /// <summary>
        /// 获取用户信息--测试token带token为system登录信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("GetUsers_By_Token_system")]
        [ProducesResponseType(201)]
        [ProducesResponseType(400)]
        [Authorize(Roles = "system")]
        // GET: api/<ValuesController>
        public Users GetUsers_By_Token_system(string name)
        {
            var db = DbHelper.GetDbConn();
            var user = db.Queryable<Users>().Where(it => it.UserName.Equals(name)).First();
            return user;
        }

        // GET api/<ValuesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<ValuesController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<ValuesController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<ValuesController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }

        /// <summary>
        /// 这是一个带参数的get请求
        /// </summary>
        /// <remarks>
        /// 例子:
        /// Get api/Values/1
        /// </remarks>
        /// <param name="id">主键</param>
        /// <returns>测试字符串</returns> 
        ///  /// <response code="201">返回value字符串</response>
        /// <response code="400">如果id为空</response> 
        [HttpGet("GetTest/{id}")]
        [ProducesResponseType(201)]
        [ProducesResponseType(400)]
        public ActionResult<string> GetTest(int id)
        {
            return $"你请求的id是{id}";
        }

        /// <summary>
        /// 新建用户
        /// </summary>
        /// <param name="user">新建用户信息</param>
        /// <returns>是否创建成功信息</returns>
        [HttpPost("add")]
        public IActionResult CreateUser([FromForm] Users user)
        {
            return Ok();
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("GetYangTestTable/{id}")]
        [ProducesResponseType(201)]
        [ProducesResponseType(400)]
        // GET: api/<ValuesController>
        public YangTestTable GetYangTestTable(string name)
        {
            var db = DbHelper.GetDbConn();
            var user = db.Queryable<YangTestTable>().Where(it => it.UserName.Equals(name)).First();
            return user;
        }

        /// <summary>
        /// --测试一条日志
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetLogInfo")]
        public string GetLogInfo()
        {
            _logger.LogInformation("测试一条日志");
            return @"路径：\bin\Debug\netcoreapp3.1\logs 下日志文件中加入了一条测试日志记录";
        }
        /// <summary>
        /// --测试读取配置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetSetting")]
        public string GetSetting()
        {
            string iiss = AppSetting.GetValue("JwtBearer:TokenName");
            return "value:" + iiss;
        }
    }
}
